package com.example.backend.controller;

import com.example.backend.domain.entity.RestBean;
import com.example.backend.domain.vo.request.ConfirmResetVO;
import com.example.backend.domain.vo.request.EmailRegisterVO;
import com.example.backend.domain.vo.request.EmailRestVO;
import com.example.backend.service.AccountService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.Pattern;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController

@RequestMapping("/api/auth")
public class AuthorizeController {
    @Resource
    AccountService service;
    @GetMapping("/ask-code")

    public RestBean<Void> askVerifyCode(@RequestParam @Email String email, @RequestParam @Pattern(regexp = "(register|reset)") String type, HttpServletRequest request) {
        String message = service.registerEmailVerifyCode(type, email, request.getRemoteAddr());
        return this.messageHandle(message);
    }

    @PostMapping("/register")
    public RestBean<Void> register(@RequestBody @Valid EmailRegisterVO vo) {
        String message = service.registerEmailAccount(vo);
        return this.messageHandle(message);
    }

    @PostMapping("/reset-confirm")
    public RestBean<Void> resetConfirm(@RequestBody @Valid ConfirmResetVO vo) {
        String message = service.resetConfirm(vo);
        return this.messageHandle(message);
    }

    @PostMapping("/reset-password")
    public RestBean<Void> resetConfirm(@RequestBody @Valid EmailRestVO vo) {
        String message = service.resetEmailAccountPassword(vo);
        return this.messageHandle(message);
    }
    private RestBean<Void> messageHandle(String action){
        String message = action;
        return message==null?RestBean.success():RestBean.failure(400,message);
    }
}
